﻿/****************************************************************************
** file: HikVisionCamera.h
** brief: 海康相机的支持代码
** Copyright (C) LiYuan
** Author: LiYuan
** E-Mail: 18069211#qq(.)com
** Version 2.0.1
** Last modified: 2021.03.20
** Modified By: LiYuan
** 已测试相机清单：MV-CE120-10UC （YUV422Packed）
****************************************************************************/

#pragma execution_character_set("utf-8")

#ifndef HIKVISION_CAMERA_H
#define HIKVISION_CAMERA_H

///
/// 海康相机的支持代码。
///
#include <VkApiDefination.h>

#include "CameraCommon/IndustryCameraInfo.h"
#include "CameraCommon/IndustryCamera.h"
#include "DialogCameraSetup.h"
#include <QRect>
#include <QString>
#include <QSettings>

namespace Qly {

class CameraVisionKit;

class CAMERA_EXPORT VisionKitCameraImageProcessing : public CameraImageProcessing
{
    friend CameraVisionKit;
    public:
    void setDevHandle(void *handle) {m_hDevHandle = handle;}
    bool setWhiteBalanceMode(WhiteBalanceMode mode) Q_DECL_OVERRIDE;
    bool whiteBalanceMode(WhiteBalanceMode &mode) Q_DECL_OVERRIDE;
    bool setWhiteBalance(int red, int green, int blue) Q_DECL_OVERRIDE;
    bool whiteBalance(int &red, int &green, int &blue) Q_DECL_OVERRIDE;
    bool autoWhiteBalance(bool &on);
    bool setWhiteBalanceRed(int red) Q_DECL_OVERRIDE;
    bool setWhiteBalanceGreen(int green) Q_DECL_OVERRIDE;
    bool setWhiteBalanceBlue(int blue) Q_DECL_OVERRIDE;
    bool whiteBalanceRedRange(int &nMin, int &nMax, int &nValue) Q_DECL_OVERRIDE;
    bool whiteBalanceGreenRange(int &nMin, int &nMax, int &nValue) Q_DECL_OVERRIDE;
    bool whiteBalanceBlueRange(int &nMin, int &nMax, int &nValue) Q_DECL_OVERRIDE;

    bool setBrightness(double value) Q_DECL_OVERRIDE;
    bool brightness(double &nValue) Q_DECL_OVERRIDE;

    bool setGamma(double v) Q_DECL_OVERRIDE;
    bool gamma(double &v) Q_DECL_OVERRIDE;
    bool gammaEnable(bool &on) Q_DECL_OVERRIDE;
    bool setGammaEnable(bool on) Q_DECL_OVERRIDE;

    bool setGainMode(GainMode mode)  Q_DECL_OVERRIDE;
    bool setGain(double fValue) Q_DECL_OVERRIDE;
    bool gain(double &fValue) Q_DECL_OVERRIDE;
    bool autoGain(bool &on);

    bool setSharpness(double nValue) Q_DECL_OVERRIDE;
    bool sharpness(double &nValue) Q_DECL_OVERRIDE;
    bool sharpnessEnable(bool &on) Q_DECL_OVERRIDE;
    bool setSharpnessEnable(bool on) Q_DECL_OVERRIDE;

    bool setBlackLevel(double nValue) Q_DECL_OVERRIDE;
    bool blackLevel(double &nValue) Q_DECL_OVERRIDE;

    bool setContrast(double nValue) Q_DECL_OVERRIDE;
    bool contrast(double &nValue)  Q_DECL_OVERRIDE;

    bool setSaturation(double nValue) Q_DECL_OVERRIDE;
    bool saturation(double &value) Q_DECL_OVERRIDE;
    bool setSaturationEnable(bool on) Q_DECL_OVERRIDE;
    bool saturationEnable(bool &on) Q_DECL_OVERRIDE;

    bool setDenoise(double value) Q_DECL_OVERRIDE;
    bool denoise(double &nValue) Q_DECL_OVERRIDE;
    bool denoiseEnable(bool &on) Q_DECL_OVERRIDE;
    bool setDenoiseEnable(bool on) Q_DECL_OVERRIDE;

    bool setHue(double nValue) Q_DECL_OVERRIDE;
    bool hue(double &value) Q_DECL_OVERRIDE;
    bool hueEnable(bool &on) Q_DECL_OVERRIDE;
    bool setHueEnable(bool on) Q_DECL_OVERRIDE;

public:
    bool gammaRange(double &fMin, double &fMax, double &fValue) Q_DECL_OVERRIDE;
    bool autoGainLowerLimitRange(double &fMin, double &fMax, double &fValue);
    bool autoGainUpperLimitRange(double &fMin, double &fMax, double &fValue);

    bool autoGainLowerLimit(double &fValue);
    bool autoGainUpperLimit(double &fValue);

    bool sharpnessRange(int &nMin, int &nMax, int &nValue);
    bool brightnessRange(int &nMin, int &nMax, int &nValue);
    bool contrastRange(int &nMin, int &nMax, int &nValue);
    bool blackLevelRange(int &nMin, int &nMax, int &nValue);
    bool gainRange(double &fMin, double &fMax, double &fValue) Q_DECL_OVERRIDE;
    bool hueRange(int &nMin, int &nMax, int &nValue);
    bool saturationRange(int &nMin, int &nMax, int &nValue);
    bool denoiseRange(int &nMin, int &nMax, int &nValue);

private:
    void * m_hDevHandle;
};


class CAMERA_EXPORT CameraVisionKit : public IndustryCamera
{
public:
    explicit CameraVisionKit(QObject *parent = nullptr);
    ~CameraVisionKit() Q_DECL_OVERRIDE;
    QString cameraName();

    bool imageSize(QSize &size, QSize &max, QSize &min);
    bool isLive() Q_DECL_OVERRIDE;
public slots:
    bool openCameraByID(QString deviceName);
    bool openDefaultCamera() Q_DECL_OVERRIDE;
    bool startGrabbing() Q_DECL_OVERRIDE;
    bool stopGrabbing() Q_DECL_OVERRIDE;
    bool setPixelType(AVPixelFormat type) Q_DECL_OVERRIDE;
    bool saveSettingsToFile(QString fileName) Q_DECL_OVERRIDE;
    bool loadSettingsFromFile(QString fileName) Q_DECL_OVERRIDE;
    void close() Q_DECL_OVERRIDE;

    bool autoWhiteBalance(bool &on);
    bool autoGain(bool &on);
    bool autoBrightness(bool &on);
    bool autoExposure(bool &on);

    //bool setAutoExposureTime(bool on) Q_DECL_OVERRIDE;
    bool setExposureTimeMode(AutoExposureMode mode) Q_DECL_OVERRIDE;
    bool exposureTimeMode(AutoExposureMode &mode) Q_DECL_OVERRIDE;

    //bool autoExposureTime(bool &on) Q_DECL_OVERRIDE;
    bool autoExposureTimeLowerLimitRange(int &nMin, int &nMax, int &nValue) Q_DECL_OVERRIDE;
    bool autoExposureTimeUpperLimitRange(int &nMin, int &nMax, int &nValue) Q_DECL_OVERRIDE;

    bool setExposureTime(double us) Q_DECL_OVERRIDE;
    bool exposureTime(double &us) Q_DECL_OVERRIDE;
    bool exposureTimeRange(double &fMin, double &fMax, double &fValue) Q_DECL_OVERRIDE;
    bool frameRate(double &fps) Q_DECL_OVERRIDE;
    bool setFrameRate(double fps) Q_DECL_OVERRIDE;

    bool showVCDPropertyPage() Q_DECL_OVERRIDE;
    bool setAOIRegion(QRect rect);
    bool setHeartBeatTimeout(int ms);

private:
    CAMERA m_hDevHandle;
    DialogCameraSetup * m_dialog;
    uint8_t m_lookupTable[4096];
    QMutex *mutex;
    void initName();
    bool registerImageCallBack();
    bool registerExceptionCallBack();
    /*相机抓图回调*/
    static void __stdcall ImageCallBack(IMAGE image, void* pUser);
    static void __stdcall MsgCallbackFunc(const EVENT_ITEM *eventItem, const unsigned char *data, unsigned int dataSize, void *context);
    static void __stdcall DisconnectCallBack(unsigned int x, void * p );

};

} //namespace Qly
#endif // HIKVISION_CAMERA_H
